<template>
  <el-drawer v-model="formCtrl.isDrawerOpen" class="cupk2-data-form" :append-to-body="true" :with-header="false"
    size="500px">
    <div class="title">{{ currentFormType.type.title }}数据</div>
    <el-form v-loading="isLoading" :model="form" label-width="auto" ref="ruleFormRef" :disabled="!formCtrl.isAbledForm">
      <el-form-item label="ID:" prop="id" v-if="isShowform.id">
        <el-input v-model="form.id" />
      </el-form-item>
      <el-form-item label="创建时间:" prop="ctime" v-if="isShowform.ctime" :rules="v.getRules(true, 0, 7)">
        <el-input v-model="form.ctime" />
      </el-form-item>
      <el-form-item label="创建用户:" prop="cuserkey" v-if="isShowform.cuserkey" :rules="v.getRules(true, 0, 16)">
        <el-input v-model="form.cuserkey" />
      </el-form-item>
      <el-form-item label="状态：" prop="state" v-if="isShowform.state" :rules="v.getRules(true)">
        <el-select v-model="form.state" placeholder="请选择">
          <el-option label="~" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注：" prop="note" v-if="isShowform.note" :rules="v.getRules(false, 0, 256)">
        <el-input v-model="form.note" autosize type="textarea" />
      </el-form-item>
      <el-form-item label="文件扩展名:" prop="exname" v-if="isShowform.exname" :rules="v.getRules(true, 0, 16)">
        <el-input v-model="form.exname" />
      </el-form-item>
      <el-form-item label="相对路径：" prop="relativepath" v-if="isShowform.relativepath" :rules="v.getRules(true, 0, 512)">
        <el-input v-model="form.relativepath" autosize type="textarea" />
      </el-form-item>
      <el-form-item label="原文件名：" prop="filename" v-if="isShowform.filename" :rules="v.getRules(true, 0, 64)">
        <el-input v-model="form.filename" autosize type="textarea" />
      </el-form-item>
      <el-form-item label="文件别名：" prop="title" v-if="isShowform.title" :rules="v.getRules(true, 0, 128)">
        <el-input v-model="form.title" autosize type="textarea" />
      </el-form-item>
      <el-form-item label="文件大小:" prop="filesize" v-if="isShowform.filesize" :rules="v.getRules(true)">
        <el-input-number v-model="form.filesize" />
      </el-form-item>
      <el-form-item label="资源地址KEY：" prop="resourcekey" v-if="isShowform.resourcekey" :rules="v.getRules(true, 0, 16)">
        <el-input v-model="form.resourcekey" autosize type="textarea" />
      </el-form-item>
      <el-form-item label="外部ID：" prop="appid" v-if="isShowform.appid" :rules="v.getRules(false, 0, 64)">
        <el-input v-model="form.appid" autosize type="textarea" />
      </el-form-item>
      <el-form-item label="存储路径：" prop="appid">
        <el-input v-model="form.fullpath" autosize type="textarea" />
      </el-form-item>
      <div style="text-align: center;"
        v-if="['jpg', 'png', 'gif'].some(element => element.toLowerCase() === form.exname)">
        <el-image style="max-width: 100%; " :src="Cupk2Request.getFullPath('api/files/download/' + form.id)"
          fit="fit" />
      </div>
      <el-form-item>
        <el-row class="cupk2-form-button">
          <el-col :span="14">
            <el-button v-if="formCtrl.isShowSubmit" type="primary" @click="onSubmitForm(addData)">提交{{
              currentFormType.type.title }}</el-button>
            <el-switch v-if="formCtrl.isShowRetain" v-model="isRetainForm" size="small" inactive-text="提交后保留表单" />
          </el-col>
          <el-col :span="10" class="cupk2-right">
            <el-button v-if="formCtrl.isShowReset" type="warning" @click="onResetForm">清空</el-button>
            <el-button v-if="formCtrl.isShowSubmit" type="info" plain @click="drawerWinOpen(false)">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>
<script lang="ts" setup>

import { inject, onMounted, reactive, ref } from 'vue';
import v from '@/hook/cupkFormValids';
import cupk2Request from '@/service/remoteRequests/Cupk2Request';
import { RequestTypeEnum } from '@/types/commons/RequestTypeEnum';
import notice from '@/components/msg/CupkNotice';
import { getCreatType, getUpdateType, getViewType, type FormParam } from '@/types/commons/FormType';
import type { DataResponse } from '@/types/commons/DataResponse';
import type { Emitter, EventType } from 'mitt';
import { PageEvent } from '@/types/commons/PageEvent';
import cupkFormUtils from '@/hook/cupkFormUtils';
import Cupk2Request from '@/service/remoteRequests/Cupk2Request';
const eventBus = inject('eventBus') as Emitter<Record<EventType, unknown>>;
const ruleFormRef = ref()
const isLoading = ref(false);
const isRetainForm = ref(false);
const currentFormType = reactive<FormParam>({ type: getViewType(), id: '', data: {} });


const formCtrl = reactive({
  isShowRetain: true,
  isShowSubmit: true,
  isDrawerOpen: false,
  isAbledForm: false,
  isShowReset: true
});


const isShowform = reactive({
  id: false,
  ctime: false,
  cuserkey: false,
  state: false,
  note: true,
  exname: true,
  relativepath: true,
  filename: true,
  title: true,
  filesize: false,
  resourcekey: true,
  appid: false,
});


const form = reactive({
  id: '',
  ctime: '',
  cuserkey: '',
  state: '',
  note: '',
  exname: '',
  relativepath: '',
  filename: '',
  title: '',
  filesize: 1,
  resourcekey: '',
  appid: '',
  fullpath: '',
})


/**
 * 打开或关闭表单窗口
 * @param isOpen
 * @param formType
 */
const drawerWinOpen = (isOpen: boolean, formParam?: FormParam) => {
  if (!isOpen) {
    formCtrl.isDrawerOpen = isOpen;
  } else {
    if (formParam?.type.key !== currentFormType?.type.key) {
      ruleFormRef.value?.resetFields();
    }
    Object.assign(currentFormType, formParam);
    if (formParam?.type.key == getCreatType().key) {
      initCreatForm(formParam);
    }
    if (formParam?.type.key == getUpdateType().key) {
      initUpdateForm(formParam.id);
    }
    if (formParam?.type.key == getViewType().key) {
      initViewForm(formParam.id);
    }
  }
};

/**
 * 初始化创建窗口
 */
const initCreatForm = (formParam: FormParam) => {
  cupkFormUtils.initFormCtrl(getCreatType(), formCtrl);
  Object.assign(isShowform, {
    ctime: false,
    cuserkey: false,
    state: false,
    note: false,
    exname: true,
    relativepath: true,
    filename: true,
    title: true,
    filesize: true,
    resourcekey: true,
    appid: false,
  });
  if (formParam) {

  }
}

/**
 * 初始化更新窗口
 * @param id
 */
const initUpdateForm = (id: string | undefined) => {
  cupkFormUtils.initFormCtrl(getUpdateType(), formCtrl);
  if (!id) {
    notice.byError("id不能为空");
  } else {
    viewData(id);
  }
}

/**
 * 初始化浏览窗口
 * @param id
 */
const initViewForm = (id: string | undefined) => {
  cupkFormUtils.initFormCtrl(getViewType(), formCtrl);
  if (!id) {
    notice.byError("id不能为空");
  } else {
    Object.assign(isShowform, {
      id: true,
      ctime: true,
      cuserkey: true,
      state: true,
      note: true,
      exname: true,
      relativepath: true,
      filename: true,
      title: true,
      filesize: true,
      resourcekey: true,
      appid: true,
    });
    viewData(id);
  }
}

/**
 * 展示信息（加载用户信息）
 */
const viewData = async (id: string) => {
  cupk2Request.submit('api/resourcefile/' + id, RequestTypeEnum.get, isLoading).then((response: DataResponse) => {
    Object.assign(form, response.data)
  }).catch((msg: string) => {
    notice.byError(msg);
  });
}

/**
 * 执行提交按钮（创建/更新）
 */
const addData = async () => {

  if (currentFormType.type.key == getCreatType().key) {
    cupk2Request.submit('api/resourcefile', RequestTypeEnum.post, isLoading, form).then(() => {
      notice.bySuccess("创建成功!");
      eventBus.emit(PageEvent.data_do_query);
      if (!isRetainForm.value) {
        onResetForm();
        drawerWinOpen(false);
      }
    }).catch((msg: string) => {
      notice.byError(msg);
    });
  }

  if (currentFormType.type.key == getUpdateType().key) {
    cupk2Request.submit('api/resourcefile/' + form.id, RequestTypeEnum.put, isLoading, form).then(() => {
      notice.bySuccess("更新成功!");
      eventBus.emit(PageEvent.data_do_query);
      onResetForm();
      drawerWinOpen(false);
    }).catch((msg: string) => {
      notice.byError(msg);
    });
  }
}


/**清空表单 */
const onResetForm = () => {
  ruleFormRef.value.resetFields();
}

/**
 * 校验表单，准备提交
 * @param func
 */
const onSubmitForm = async (func: () => void) => {
  await ruleFormRef.value.validate((valid: boolean) => { if (valid) { func() } });
}

onMounted(() => {
  eventBus.on(PageEvent.data_form_open, (paras) => {
    drawerWinOpen((paras as { isOpen: boolean }).isOpen, (paras as { type: FormParam }).type);
  })
});
</script>
